package service;

import java.util.List;

import play.db.jpa.Model;
import models.AutTra;
import models.Book;
import models.BookType;
import models.Comment;

public class BookService {

	/**
	 * 读取分类下的所有分类
	 * @param fid
	 * @return 子分类str ,号隔开
	 */
	public static String getSunType(Long fid){
		String str = "";
		List<BookType> sb = BookType.find("fid=?", fid).fetch();
		for(BookType t : sb){
			str+=","+t.getId()+ getSunType(t.id);
		}
		return str;
	}
	
	/**
	 * 获得书籍的主编
	 * @param booid
	 * @return
	 */
	public static AutTra getAuthor(Long booid){
		AutTra a = null;
		try{
			a = AutTra.find("booId=? and autrType = 1",booid).first();
		}catch(Exception e){}
		return a;
	}
	
	/**
	 * 获得书籍的所有作者
	 * @param booid
	 * @return
	 */
	public static List<AutTra> getAuthors(Long booid){
		List<AutTra> as = null;
		try{
			as = AutTra.find("booId=?",booid).fetch();
		}catch(Exception e){}
		return as;
	}
	
	/**
	 * 获取图书星级
	 * @param id
	 * @return
	 */
	public static Double pinXingJi(Long id){
		StringBuffer str = new StringBuffer();
		Double xj = 0d;
		int r = 0;
		try{
			List<Comment> cs= CommentService.getComment(id);
			for(Comment c : cs){
				xj += c.xj;
				r++;
			}
		}catch(Exception e){}
		if(r!=0)
			xj = xj/(r*5);
		else
			xj = 0d;
		
		return xj*60;
	}
	
	/**
	 * 热评读书
	 * @param size 读取条数
	 * @return
	 */
	public static List<Book> getHootPin(int size){
		return Book.find("order by booPin desc").fetch(0,size);
	}
	
	/**
	 * 网站推荐
	 * @param size 读取条数
	 * @return
	 */
	public static List<Book> getHootTui(int size){
		return Book.find("order by booPin desc").fetch(0,size);
	}
	
	/**
	 * 网站推荐按类别
	 * @param size 读取数量
	 * @param typeId 图书类型
	 * @return
	 */
	public static List<Book> getHootTuiByType(int size,Long typeId){
		return Book.find(" bookTypeId=? order by booPin desc",typeId).fetch(0,size);
	}
	
	/**
	 * 新书上架
	 * @param size 读取条数
	 * @return
	 */
	public static List<Book> getNewBook(int size){
		return Book.find("order by booPin desc").fetch(0,size);
	}
	
	/**
	 * 收藏榜
	 * @param size
	 * @return
	 */
	public static List<Book> getEnshBook(int size){
		return Book.find("order by booEnsh desc").fetch(0,size);
	}
	
	/**
	 * 借阅榜
	 * @param size
	 * @return
	 */
	public static List<Book> getBorrBook(int size){
		return Book.find("order by booBorr desc").fetch(0,size);
	}
	
	/**
	 * 图书类型
	 * 
	 */
	public static BookType finBookType(String typeid){
		BookType booktype  = null;
		booktype = BookType.find("from BookType where fid = ?",Long.parseLong(typeid)).first();
		return booktype;
	}
	
	/**
	 * 指定内型下的所有图书
	 * 
	 */
	public static List<Book> allBook(String bookTypeIds){
		System.out.println(bookTypeIds+"||||||||||||||||||||||||||||||||||||||||||||||");
		List<Book> booklist = Book.find("from Book where bookTypeIds = ?", bookTypeIds).fetch();
		System.out.println("||||||||||||||||||||||||||||||||||||||||||||||"+booklist);
		return booklist ;
	}
}
